﻿namespace LevelEditors
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text.RegularExpressions;
    using UnityEngine;
    using UnityEngine.UI;

    public class UIPrefabBrowserCategoryGroup : MonoBehaviour
    {
        private List<UIElement> contents = new List<UIElement>();
        private HashSet<string> expandedPaths = new HashSet<string>();
        private bool groupByAuthor;
        public static UIPrefabBrowserCategoryGroup Instance;
        [SerializeField]
        private UIElement m_contentPrefab;
        [SerializeField]
        private Transform m_group;
        [SerializeField]
        private Button m_optionsMenuOpenButton;
        private ToggleGroup toggleGroup;
        private TreeView treeView = new TreeView();

        private void Awake()
        {
            char[] separator = new char[] { ';' };
            this.expandedPaths = new HashSet<string>(PlayerPrefs.GetString("UIPrefabBrowser.expandedPaths").Split(separator));
            this.groupByAuthor = PlayerPrefs.GetInt("UIPrefabBrowser.groupByAuthor") == 1;
            Instance = this;
            this.toggleGroup = this.m_group.GetComponent<ToggleGroup>();
            LevelEditor.Instance.onLoadLevel += new Action(this.Refresh);
            this.m_optionsMenuOpenButton.onClick.AddListener(delegate {
                UIGenericMenu.Clear();
                UIGenericMenu.Add("Group By Author", this.groupByAuthor, delegate {
                    this.groupByAuthor = !this.groupByAuthor;
                    this.Start();
                    this.Refresh();
                });
                UIGenericMenu.Show();
            });
        }

        private string GetCatetory(string fullName)
        {
            return (!fullName.Contains("/") ? "^[^/]*$" : fullName.Substring(0, fullName.LastIndexOf("/") + 1));
        }

        private UIElement GetContent(string name)
        {
            <GetContent>c__AnonStorey42 storey = new <GetContent>c__AnonStorey42 {
                name = name
            };
            return this.contents.Find(new Predicate<UIElement>(storey.<>m__33));
        }

        private bool IsMatch(string input, string pattern)
        {
            try
            {
                return Regex.IsMatch(input, pattern);
            }
            catch (Exception)
            {
            }
            return false;
        }

        private void OnDestroy()
        {
            PlayerPrefs.SetString("UIPrefabBrowser.expandedPaths", string.Join(";", this.expandedPaths.ToArray<string>()));
            PlayerPrefs.SetInt("UIPrefabBrowser.groupByAuthor", !this.groupByAuthor ? 0 : 1);
        }

        private void Refresh()
        {
            <Refresh>c__AnonStorey43 storey = new <Refresh>c__AnonStorey43 {
                <>f__this = this
            };
            if (LevelEditor.Instance.hasLevelSelected)
            {
                storey.nodes = this.treeView.GetTreeNodes().ToList<TreeView.Node>();
                UIUtility.RefreshElements<UIElement>(this.contents, storey.nodes.Count<TreeView.Node>(), this.m_contentPrefab, this.m_group, new Action<UIElement, int>(storey.<>m__35));
            }
        }

        private void Start()
        {
            this.treeView = new TreeView();
            foreach (Asset asset in AssetManager.GetAllPrefabAssets())
            {
                string category = Regex.Replace(asset.categoryWithoutAuthor, "^Prefabs/", string.Empty);
                if (this.groupByAuthor)
                {
                    category = asset.category;
                }
                this.treeView.AddPath(category);
            }
            this.treeView.GetAllNodes().Foreach<TreeView.Node>((Action<TreeView.Node>) (x => (x.expanded = this.expandedPaths.Contains(x.path))));
            this.Refresh();
        }

        [CompilerGenerated]
        private sealed class <GetContent>c__AnonStorey42
        {
            internal string name;

            internal bool <>m__33(UIElement x)
            {
                return (x.name == this.name);
            }
        }

        [CompilerGenerated]
        private sealed class <Refresh>c__AnonStorey43
        {
            internal UIPrefabBrowserCategoryGroup <>f__this;
            internal List<TreeView.Node> nodes;

            internal void <>m__35(UIElement c, int i)
            {
                <Refresh>c__AnonStorey44 storey = new <Refresh>c__AnonStorey44 {
                    <>f__ref$67 = this,
                    c = c,
                    node = this.nodes[i]
                };
                storey.c.group = this.<>f__this.toggleGroup;
                storey.c.index = i;
                storey.c.name = storey.node.path;
                string str = string.Empty;
                if (storey.node.children.Count > 0)
                {
                    str = !storey.node.expanded ? "+" : "-";
                }
                storey.c.label = new string('\t', storey.node.indentLevel) + str + storey.node.name.Localize();
                storey.c.onRightClick = new Action(storey.<>m__37);
                storey.c.isOn = !string.IsNullOrEmpty(UIPrefabBrowser.Instance.category) && (UIPrefabBrowser.Instance.category == storey.c.name);
                storey.c.onSelected = new Action(storey.<>m__38);
            }

            private sealed class <Refresh>c__AnonStorey44
            {
                internal UIPrefabBrowserCategoryGroup.<Refresh>c__AnonStorey43 <>f__ref$67;
                internal UIElement c;
                internal TreeView.Node node;

                internal void <>m__37()
                {
                    this.node.expanded = !this.node.expanded;
                    this.<>f__ref$67.<>f__this.Refresh();
                    if (this.node.expanded)
                    {
                        this.<>f__ref$67.<>f__this.expandedPaths.Add(this.node.path);
                    }
                    else
                    {
                        this.<>f__ref$67.<>f__this.expandedPaths.Remove(this.node.path);
                    }
                }

                internal void <>m__38()
                {
                    UIPrefabBrowser.Instance.category = this.c.name;
                }
            }
        }
    }
}

